//------------------------------------------------------------------------------
// X2D Game Engine
// Copyright (C) 2006-2007 Janusz Kowalski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#include "x2d_editMode.h"
#include "x2d_main.h"
#include "imgSDL.h"
#include "mainLoop.h"
#include "x2d_New.h"
#include "global.h"
#include "x2tk_Container.h"
#include "x2tk.h"
#include "pmsg.h"

namespace x2d {

class Editor : public x2tk::Container {
  private:
    Map *map_;
    EditView *view_;
    World *world_;
    int nextTime_;
  public:
    Editor(int x, int y, int w, int h) : x2tk::Container(x,y,w,h) {
      map_ = new x2d::Map(225, 32);
      world_ = new World(map_);
      view_= new x2d::EditView(0,32,getScreenWidget()->getW(),getScreenWidget()->getH()-64, world_);
      view_->follow(0);
      add(view_);
      //add(view_);
      nextTime_=SDL_GetTicks();
    }
    virtual ~Editor() {
      delete view_;
      delete world_;
      delete map_;
    }
    virtual void timed(int id) {
      int currentTime = SDL_GetTicks();
      while(nextTime_<=currentTime) {
        view_->move();
        // Here we add 10 ms (because we want physics system to work at 100Hz)
        nextTime_+=10;
        
        world_->timeCb();
      }
    }
};

Editor *editor;

void editModeStart() {
  editor = new Editor(0,0,getScreenWidget()->getW(),getScreenWidget()->getH());
  getScreenWidget()->add(editor);
  callRepetitively(editor, 0);
}

void editModeEnd() {
  pmsgDebugF("Editor %p is going donw\n", editor);
  delete editor;
}

}

